package com.breakfast.fun.ui.user.order;

import android.content.Intent;
import android.view.View;
import android.widget.ImageView;
import android.widget.ListView;

import com.breakfast.fun.R;
import com.breakfast.fun.adapter.user.UserBFOrderAdapter;
import com.breakfast.fun.base.BaseFragment;
import com.breakfast.fun.data.entity.user.order.Order;
import com.breakfast.fun.data.entity.user.recharge.PayBean;
import com.breakfast.fun.event.PayEvent;
import com.breakfast.fun.event.RefreshEvent;
import com.breakfast.fun.event.ShowCouponEvent;
import com.breakfast.fun.ui.user.pay.BFPayActivity;
import com.breakfast.fun.util.T;
import com.handmark.pulltorefresh.library.PullToRefreshBase;
import com.handmark.pulltorefresh.library.PullToRefreshListView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;

import butterknife.Bind;

/**
 * Created by Administrator on 2016/8/29.
 */
public class BFOrderFragment extends BaseFragment<BFOrderPresenter, BFOrderModel> implements BFOrderContract.View {


    @Bind(R.id.ptrlv_bforder)
    PullToRefreshListView ptrlvBforder;
    @Bind(R.id.iv_bforder_empty)
    ImageView ivBforderEmpty;

    private UserBFOrderAdapter adapter;

    private int begin = 0;

    @Override
    public int getLayoutId() {
        return R.layout.fragment_user_bforder;
    }

    @Override
    public void initView() {
        EventBus.getDefault().register(this);
        ptrlvBforder.setMode(PullToRefreshBase.Mode.BOTH);
        ptrlvBforder.setOnRefreshListener(new PullToRefreshBase.OnRefreshListener2<ListView>() {
            @Override
            public void onPullDownToRefresh(PullToRefreshBase<ListView> refreshView) {
                begin = 0;
                presenter.getBFOrder();
            }

            @Override
            public void onPullUpToRefresh(PullToRefreshBase<ListView> refreshView) {
                presenter.loadBFOrder(begin);
            }
        });

    }

    @Override
    public void showBFOrder(Order order) {
        if (order.getData().size() == 0) {
            ivBforderEmpty.setVisibility(View.VISIBLE);
        } else {
            ivBforderEmpty.setVisibility(View.GONE);
            begin = 10;
        }
        adapter = new UserBFOrderAdapter(context, order.getData());
        ptrlvBforder.setAdapter(adapter);

        EventBus.getDefault().post(new ShowCouponEvent(true));
    }

    @Override
    public void loadBFOrder(Order order) {
        if (order.getData().size() == 0) {
            T.show("已无更多数据");
        } else {
            begin += 10;
            adapter.addALL(order.getData());
            adapter.notifyDataSetChanged();
        }
    }

    @Override
    public void toPay(PayBean payBean) {
        Intent intent = new Intent(getActivity(), BFPayActivity.class);
        intent.putExtra("payBean", payBean);
        startActivity(intent);
    }

    @Override
    public void stopRefresh() {
        ptrlvBforder.onRefreshComplete();
    }

    @Subscribe
    public void onEventPay(PayEvent event) {
        if (event.getClassName().equals(this.getClass().getSimpleName())) {
            presenter.getPayBean(event.getOrderNo());
        }
    }

    @Subscribe
    public void onEvent(RefreshEvent event) {
        if (event.getActivityName().equals(getClass().getSimpleName())) {
            presenter.getBFOrder();
        }
    }

    @Override
    public void onDestroyView() {
        super.onDestroyView();
        EventBus.getDefault().unregister(this);
    }
}
